﻿namespace Hidistro.ControlPanel.Promotions
{
    using Hidistro.Core;
    using Hidistro.Core.Entities;
    using Hidistro.Entities.Promotions;
    using Hidistro.Membership.Context;
    using Hidistro.Membership.Core;
    using Hidistro.Membership.Core.Enums;
    using Hidistro.SqlDal.Comments;
    using Hidistro.SqlDal.Promotions;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using Hidistro.Entities.Members;

    public static class PromoteHelper
    {
        public static bool AddBundlingProduct(BundlingInfo bundlingInfo)
        {
            bool flag;
            using (DbConnection connection = DatabaseFactory.CreateDatabase().CreateConnection())
            {
                connection.Open();
                DbTransaction dbTran = connection.BeginTransaction();
                try
                {
                    BundlingDao dao = new BundlingDao();
                    int bundlingID = dao.AddBundlingProduct(bundlingInfo, dbTran);
                    if (bundlingID <= 0)
                    {
                        dbTran.Rollback();
                        return false;
                    }
                    if (!dao.AddBundlingProductItems(bundlingID, bundlingInfo.BundlingItemInfos, dbTran))
                    {
                        dbTran.Rollback();
                        return false;
                    }
                    dbTran.Commit();
                    flag = true;
                }
                catch (Exception)
                {
                    dbTran.Rollback();
                    flag = false;
                }
                finally
                {
                    connection.Close();
                }
            }
            return flag;
        }

        public static bool AddCountDown(CountDownInfo countDownInfo)
        {
            return new CountDownDao().AddCountDown(countDownInfo);
        }

        public static bool AddGroupBuy(GroupBuyInfo groupBuy)
        {
            bool flag;
            Globals.EntityCoding(groupBuy, true);
            using (DbConnection connection = DatabaseFactory.CreateDatabase().CreateConnection())
            {
                connection.Open();
                DbTransaction dbTran = connection.BeginTransaction();
                try
                {
                    GroupBuyDao dao = new GroupBuyDao();
                    int groupBuyId = dao.AddGroupBuy(groupBuy, dbTran);
                    if (groupBuyId <= 0)
                    {
                        dbTran.Rollback();
                        return false;
                    }
                    if (!dao.AddGroupBuyCondition(groupBuyId, groupBuy.GroupBuyConditions, dbTran))
                    {
                        dbTran.Rollback();
                        return false;
                    }
                    dbTran.Commit();
                    flag = true;
                }
                catch (Exception)
                {
                    dbTran.Rollback();
                    flag = false;
                }
                finally
                {
                    connection.Close();
                }
            }
            return flag;
        }

        public static int AddPromotion(PromotionInfo promotion)
        {
            int num2;
            using (DbConnection connection = DatabaseFactory.CreateDatabase().CreateConnection())
            {
                connection.Open();
                DbTransaction dbTran = connection.BeginTransaction();
                try
                {
                    PromotionDao dao = new PromotionDao();
                    int activityId = dao.AddPromotion(promotion, dbTran);
                    if (activityId <= 0)
                    {
                        dbTran.Rollback();
                        return -1;
                    }
                    if (!dao.AddPromotionMemberGrades(activityId, promotion.MemberGradeIds, dbTran))
                    {
                        dbTran.Rollback();
                        return -2;
                    }
                    dbTran.Commit();
                    num2 = activityId;
                }
                catch (Exception)
                {
                    dbTran.Rollback();
                    num2 = 0;
                }
                finally
                {
                    connection.Close();
                }
            }
            return num2;
        }

        public static bool AddPromotionProducts(int activityId, string productIds)
        {
            return new PromotionDao().AddPromotionProducts(activityId, productIds);
        }

        public static bool DeleteBundlingProduct(int bundlingID)
        {
            return new BundlingDao().DeleteBundlingProduct(bundlingID);
        }

        public static bool DeleteCountDown(int countDownId)
        {
            return new CountDownDao().DeleteCountDown(countDownId);
        }

        public static bool DeleteGroupBuy(int groupBuyId)
        {
            return new GroupBuyDao().DeleteGroupBuy(groupBuyId);
        }

        public static bool DeletePromotion(int activityId)
        {
            return new PromotionDao().DeletePromotion(activityId);
        }

        public static bool DeletePromotionProducts(int activityId, int? productId)
        {
            return new PromotionDao().DeletePromotionProducts(activityId, productId);
        }

        public static int EditPromotion(PromotionInfo promotion)
        {
            int num;
            using (DbConnection connection = DatabaseFactory.CreateDatabase().CreateConnection())
            {
                connection.Open();
                DbTransaction dbTran = connection.BeginTransaction();
                try
                {
                    PromotionDao dao = new PromotionDao();
                    if (!dao.EditPromotion(promotion, dbTran))
                    {
                        dbTran.Rollback();
                        return -1;
                    }
                    if (!dao.AddPromotionMemberGrades(promotion.ActivityId, promotion.MemberGradeIds, dbTran))
                    {
                        dbTran.Rollback();
                        return -2;
                    }
                    dbTran.Commit();
                    num = 1;
                }
                catch (Exception)
                {
                    dbTran.Rollback();
                    num = 0;
                }
                finally
                {
                    connection.Close();
                }
            }
            return num;
        }

        public static BundlingInfo GetBundlingInfo(int bundlingID)
        {
            return new BundlingDao().GetBundlingInfo(bundlingID);
        }

        public static DbQueryResult GetBundlingProducts(BundlingInfoQuery query)
        {
            return new BundlingDao().GetBundlingProducts(query);
        }

        public static CountDownInfo GetCountDownInfo(int countDownId)
        {
            return new CountDownDao().GetCountDownInfo(countDownId);
        }

        public static DbQueryResult GetCountDownList(GroupBuyQuery query)
        {
            return new CountDownDao().GetCountDownList(query);
        }

        public static decimal GetCurrentPrice(int groupBuyId, int prodcutQuantity)
        {
            return new GroupBuyDao().GetCurrentPrice(groupBuyId, prodcutQuantity);
        }

        public static GroupBuyInfo GetGroupBuy(int groupBuyId)
        {
            return new GroupBuyDao().GetGroupBuy(groupBuyId);
        }

        public static DbQueryResult GetGroupBuyList(GroupBuyQuery query)
        {
            return new GroupBuyDao().GetGroupBuyList(query);
        }

        public static IList<Member> GetMembersByRank(int? gradeId)
        {
            return new MessageBoxDao().GetMembersByRank(gradeId);
        }

        public static IList<Member> GetMemdersByNames(IList<string> names)
        {
            IList<Member> list = new List<Member>();
            foreach (string str in names)
            {
                IUser user = Users.GetUser(0, str, false, false);
                if ((user != null) && (user.UserRole == UserRole.Member))
                {
                    list.Add(user as Member);
                }
            }
            return list;
        }

        public static int GetOrderCount(int groupBuyId)
        {
            return new GroupBuyDao().GetOrderCount(groupBuyId);
        }

        public static string GetPriceByProductId(int productId)
        {
            return new GroupBuyDao().GetPriceByProductId(productId);
        }

        public static IList<MemberGradeInfo> GetPromoteMemberGrades(int activityId)
        {
            return new PromotionDao().GetPromoteMemberGrades(activityId);
        }

        public static PromotionInfo GetPromotion(int activityId)
        {
            return new PromotionDao().GetPromotion(activityId);
        }

        public static PromotionInfo GetPromotionByProduct(int productId)
        {
            PromotionInfo promotion = null;
            int? activeIdByProduct = new PromotionDao().GetActiveIdByProduct(productId);
            if (activeIdByProduct.HasValue)
            {
                promotion = GetPromotion(activeIdByProduct.Value);
            }
            return promotion;
        }

        public static DataTable GetPromotionProducts(int activityId)
        {
            return new PromotionDao().GetPromotionProducts(activityId);
        }

        public static DataTable GetPromotions(bool isProductPromote, bool isWholesale)
        {
            return new PromotionDao().GetPromotions(isProductPromote, isWholesale);
        }

        public static bool ProductCountDownExist(int productId)
        {
            return new CountDownDao().ProductCountDownExist(productId);
        }

        public static bool ProductGroupBuyExist(int productId)
        {
            return new GroupBuyDao().ProductGroupBuyExist(productId);
        }

        public static bool SetGroupBuyEndUntreated(int groupBuyId)
        {
            return new GroupBuyDao().SetGroupBuyEndUntreated(groupBuyId);
        }

        public static bool SetGroupBuyStatus(int groupBuyId, GroupBuyStatus status)
        {
            return new GroupBuyDao().SetGroupBuyStatus(groupBuyId, status);
        }

        public static void SwapCountDownSequence(int countDownId, int displaySequence)
        {
            new CountDownDao().SwapCountDownSequence(countDownId, displaySequence);
        }

        public static void SwapGroupBuySequence(int groupBuyId, int displaySequence)
        {
            new GroupBuyDao().SwapGroupBuySequence(groupBuyId, displaySequence);
        }

        public static bool UpdateBundlingProduct(BundlingInfo bundlingInfo)
        {
            bool flag;
            using (DbConnection connection = DatabaseFactory.CreateDatabase().CreateConnection())
            {
                connection.Open();
                DbTransaction dbTran = connection.BeginTransaction();
                try
                {
                    BundlingDao dao = new BundlingDao();
                    if (!dao.UpdateBundlingProduct(bundlingInfo, dbTran))
                    {
                        dbTran.Rollback();
                        return false;
                    }
                    if (!dao.DeleteBundlingByID(bundlingInfo.BundlingID, dbTran))
                    {
                        dbTran.Rollback();
                        return false;
                    }
                    if (!dao.AddBundlingProductItems(bundlingInfo.BundlingID, bundlingInfo.BundlingItemInfos, dbTran))
                    {
                        dbTran.Rollback();
                        return false;
                    }
                    dbTran.Commit();
                    flag = true;
                }
                catch (Exception)
                {
                    dbTran.Rollback();
                    flag = false;
                }
                finally
                {
                    connection.Close();
                }
            }
            return flag;
        }

        public static bool UpdateCountDown(CountDownInfo countDownInfo)
        {
            return new CountDownDao().UpdateCountDown(countDownInfo);
        }

        public static bool UpdateGroupBuy(GroupBuyInfo groupBuy)
        {
            bool flag;
            Globals.EntityCoding(groupBuy, true);
            using (DbConnection connection = DatabaseFactory.CreateDatabase().CreateConnection())
            {
                connection.Open();
                DbTransaction dbTran = connection.BeginTransaction();
                try
                {
                    GroupBuyDao dao = new GroupBuyDao();
                    if (!dao.UpdateGroupBuy(groupBuy, dbTran))
                    {
                        dbTran.Rollback();
                        return false;
                    }
                    if (!dao.DeleteGroupBuyCondition(groupBuy.GroupBuyId, dbTran))
                    {
                        dbTran.Rollback();
                        return false;
                    }
                    if (!dao.AddGroupBuyCondition(groupBuy.GroupBuyId, groupBuy.GroupBuyConditions, dbTran))
                    {
                        dbTran.Rollback();
                        return false;
                    }
                    dbTran.Commit();
                    flag = true;
                }
                catch (Exception)
                {
                    dbTran.Rollback();
                    flag = false;
                }
                finally
                {
                    connection.Close();
                }
            }
            return flag;
        }
    }
}

